#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "php_monitorpro.h"
#include "helper.h"
#include "init.h"

ZEND_DECLARE_MODULE_GLOBALS(monitorpro)

char *logdir = nullptr;

PHP_INI_BEGIN()
STD_PHP_INI_ENTRY("monitorpro.debug", "0", PHP_INI_ALL, OnUpdateBool, debug, zend_monitorpro_globals, monitorpro_globals)
STD_PHP_INI_ENTRY("monitorpro.enable", "1", PHP_INI_ALL, OnUpdateBool, enable, zend_monitorpro_globals, monitorpro_globals)
// STD_PHP_INI_ENTRY("monitorpro.skyenable", "1", PHP_INI_ALL, OnUpdateBool, skyenable, zend_monitorpro_globals, monitorpro_globals)
// STD_PHP_INI_ENTRY("monitorpro.skyversion", "8", PHP_INI_ALL, OnUpdateLong, skyversion, zend_monitorpro_globals, monitorpro_globals)
// STD_PHP_INI_ENTRY("monitorpro.skyagentip", "127.0.0.1", PHP_INI_ALL, OnUpdateString, skyagentip, zend_monitorpro_globals, monitorpro_globals)
// STD_PHP_INI_ENTRY("monitorpro.skyagentport", "9999", PHP_INI_ALL, OnUpdateString, skyagentport, zend_monitorpro_globals, monitorpro_globals)
// STD_PHP_INI_ENTRY("monitorpro.skyqueuesize", "0", PHP_INI_ALL, OnUpdateLong, skyqueuesize, zend_monitorpro_globals, monitorpro_globals)
STD_PHP_INI_ENTRY("monitorpro.pushenable", "1", PHP_INI_ALL, OnUpdateBool, pushenable, zend_monitorpro_globals, monitorpro_globals)
// STD_PHP_INI_ENTRY("monitorpro.pushinterval", "15", PHP_INI_ALL, OnUpdateLong, pushinterval, zend_monitorpro_globals, monitorpro_globals)
// STD_PHP_INI_ENTRY("monitorpro.pushtimeout", "3", PHP_INI_ALL, OnUpdateLong, pushtimeout, zend_monitorpro_globals, monitorpro_globals)
// STD_PHP_INI_ENTRY("monitorpro.pushconnecttimeout", "3", PHP_INI_ALL, OnUpdateLong, pushconnecttimeout, zend_monitorpro_globals, monitorpro_globals)
// STD_PHP_INI_ENTRY("monitorpro.pushhost", "http://pushgateway-dev.39on.com", PHP_INI_ALL, OnUpdateString, pushhost, zend_monitorpro_globals, monitorpro_globals)
STD_PHP_INI_ENTRY("monitorpro.pushbucket", "0.0005,0.001,0.0025,0.005,0.01,0.025,0.05,0.1,0.25,0.5,1,2.5,5,10", PHP_INI_ALL, OnUpdateString, pushbucket, zend_monitorpro_globals, monitorpro_globals)
STD_PHP_INI_ENTRY("monitorpro.pushcollect", "/tmp/pushgateway_collect.log", PHP_INI_ALL, OnUpdateString, pushcollect, zend_monitorpro_globals, monitorpro_globals)
STD_PHP_INI_ENTRY("monitorpro.pushcollectsize", "1", PHP_INI_ALL, OnUpdateLong, pushcollectsize, zend_monitorpro_globals, monitorpro_globals)
STD_PHP_INI_ENTRY("monitorpro.pushbuf", "0", PHP_INI_ALL, OnUpdateLong, pushbuf, zend_monitorpro_globals, monitorpro_globals)
STD_PHP_INI_ENTRY("monitorpro.pushflushinterval", "0", PHP_INI_ALL, OnUpdateLong, pushflushinterval, zend_monitorpro_globals, monitorpro_globals)
STD_PHP_INI_ENTRY("monitorpro.log", "/tmp/monintorpro.log", PHP_INI_ALL, OnUpdateString, log, zend_monitorpro_globals, monitorpro_globals)
PHP_INI_END()

static void php_monitorpro_init_globals(zend_monitorpro_globals *monitorpro_globals)
{
	monitorpro_globals->debug = 0;
	monitorpro_globals->enable = 0;
	// monitorpro_globals->skyenable = 0;
	// monitorpro_globals->skyversion = 8;
	// monitorpro_globals->skyagentip = nullptr;
	// monitorpro_globals->skyagentport = nullptr;
	// monitorpro_globals->skyqueuesize = 0;
	monitorpro_globals->pushenable = 0;
	// monitorpro_globals->pushinterval = 0;
	// monitorpro_globals->pushtimeout = 0;
	// monitorpro_globals->pushconnecttimeout = 0;
	// monitorpro_globals->pushhost = nullptr;
	monitorpro_globals->pushbucket = nullptr;
	monitorpro_globals->pushcollect = nullptr;
	monitorpro_globals->pushcollectsize = 1;
	monitorpro_globals->pushbuf = 0;
	monitorpro_globals->pushflushinterval = 0;
	monitorpro_globals->log = nullptr;
}

PHP_FUNCTION(helloworld)
{
	php_printf("hello world!\n");

	RETURN_TRUE;
}

PHP_MINIT_FUNCTION(monitorpro)
{
	// 初始化模块配置
	ZEND_INIT_MODULE_GLOBALS(monitorpro, php_monitorpro_init_globals, NULL);
	// 读取 php.ini 配置文件
	REGISTER_INI_ENTRIES();

	if (MONITORPRO_G(enable))
	{
		logdir = MONITORPRO_G(log);
		logger("加载模块");
		init_minit();
	}

	return SUCCESS;
}

PHP_MSHUTDOWN_FUNCTION(monitorpro)
{
	UNREGISTER_INI_ENTRIES();

	if (MONITORPRO_G(enable))
	{
		init_mshutdown();
		logger("退出模块");
	}

	return SUCCESS;
}

PHP_RINIT_FUNCTION(monitorpro)
{
#if defined(COMPILE_DL_MONITORPRO) && defined(ZTS)
	ZEND_TSRMLS_CACHE_UPDATE();
#endif

	if (MONITORPRO_G(enable))
	{
		logger("请求开始");
		init_rinit();
	}

	return SUCCESS;
}

PHP_RSHUTDOWN_FUNCTION(monitorpro)
{
	if (MONITORPRO_G(enable))
	{
		init_rshutdown();
		logger("请求结束");
	}

	return SUCCESS;
}

PHP_MINFO_FUNCTION(monitorpro)
{
	php_info_print_table_start();
	php_info_print_table_header(2, "monitorpro support", "enabled");
	php_info_print_table_end();

	DISPLAY_INI_ENTRIES();
}

const zend_function_entry monitorpro_functions[] = {
	PHP_FE(helloworld, NULL) /* For testing, remove later. */
	PHP_FE_END				 /* Must be the last line in monitorpro_functions[] */
};

zend_module_entry monitorpro_module_entry = {
	STANDARD_MODULE_HEADER,
	"monitorpro",
	monitorpro_functions,
	PHP_MINIT(monitorpro),
	PHP_MSHUTDOWN(monitorpro),
	PHP_RINIT(monitorpro),	   /* Replace with NULL if there's nothing to do at request start */
	PHP_RSHUTDOWN(monitorpro), /* Replace with NULL if there's nothing to do at request end */
	PHP_MINFO(monitorpro),
	PHP_MONITORPRO_VERSION,
	STANDARD_MODULE_PROPERTIES};

#ifdef COMPILE_DL_MONITORPRO
#ifdef ZTS
ZEND_TSRMLS_CACHE_DEFINE()
#endif
ZEND_GET_MODULE(monitorpro)
#endif